package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.product.ProductSkuDetailFeignClient;
import com.atguigu.gmall.product.vo.SkuDetailVo;
import com.atguigu.gmall.feign.item.SkuDetailFeignClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;

@Api(tags = "远程调用-商品详情")//web-all===>service-item===>service-product
@Controller
public class ItemController {

    @Autowired
    SkuDetailFeignClient skuDetailFeignClient;
    @Autowired
    ProductSkuDetailFeignClient productSkuDetailFeignClient;

    @GetMapping("/{skuId}.html")
    public String itemPage(@PathVariable("skuId") Long skuId, Model model){
        //todo 远程调用 service-item ，再远程调用 service-product 获取商品数据详情
        Result<SkuDetailVo> skuDetails = skuDetailFeignClient.getSkuDetails(skuId);//远程调用 返回json数据，转化为Result类型
        SkuDetailVo detailVo = skuDetails.getData();

        //1 分类视图 {category1Id、category2Id、category3Id、category1Name、category2Name、category3Name}
        model.addAttribute("categoryView", detailVo.getCategoryView());

        //2 sku信息 {基本信息、图片列表}
        model.addAttribute("skuInfo", detailVo.getSkuInfo());

        //3 实时价格
        BigDecimal bigDecimal = productSkuDetailFeignClient.getPrice(skuId).getData();
        model.addAttribute("price", bigDecimal);
        //model.addAttribute("price", detailVo.getPrice());2

        //4 所有销售属性集合
        model.addAttribute("spuSaleAttrList", detailVo.getSpuSaleAttrList());

        //5 valuesSkuJson
        model.addAttribute("valuesSkuJson", detailVo.getValuesSkuJson());

        //todo 6 sku的规格；平台属性
        return "item/index";//跳转到首页
    }
}
